package com.chinamsp.dydb.query;

/**
 * 分页对象
 * @author shifenghu
 *
 */
public class Pagination implements java.io.Serializable {
	private static final long serialVersionUID = 1L;
	/**
	 * 默认每页条数
	 */
	public static final int DEFAULT_PAGE_SIZE = 50;
	
	/**
	 * 默认当前页
	 */
	public static final int DEFAULT_CURRENTPAGE = 1;

	private int pageSize; // 每页默认10条数据
	private int currentPage; // 当前页
	private int totalPages; // 总页数
	private int totalCount; // 总数据数

	/**
	 * 构造一个 自定义 每页条数，当前页数的分页对象
	 * @param pageSize
	 * @param currentPage
	 */
	public Pagination(int pageSize, int currentPage) {
		this.pageSize = pageSize > 0 ? pageSize : DEFAULT_PAGE_SIZE;
		this.currentPage = currentPage > 0 ? currentPage : DEFAULT_CURRENTPAGE;
	}

	/**
	 * 构造一个默认分页数量{@link #DEFAULT_PAGE_SIZE}}和当前页{@link #DEFAULT_CURRENTPAGE}}的分页对象
	 */
	public Pagination() {
		this.pageSize = DEFAULT_PAGE_SIZE;
		this.currentPage = DEFAULT_CURRENTPAGE;
	}

	/**
	 * 初始化分页参数:需要先设置totalRows
	 */

	public Pagination init(int totalCount, int pageSize) {
		this.pageSize = pageSize;
		this.totalCount = totalCount;
		if ((totalCount % pageSize) == 0) {
			totalPages = totalCount / pageSize;
		} else {
			totalPages = totalCount / pageSize + 1;
		}
		return this;
	}

	/**
	 * 初始化一个分页对象
	 * @param totalCount
	 * @param pageSize
	 * @param currentPage
	 * @return
	 */
	public Pagination init(int totalCount, int pageSize, int currentPage) {
		this.currentPage = currentPage;
		return this.init(totalCount, pageSize);
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		pageSize = pageSize > 0 ? pageSize : DEFAULT_PAGE_SIZE;
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		currentPage = currentPage > 0 ? currentPage : DEFAULT_CURRENTPAGE;
		this.currentPage = currentPage;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	
	/**
	 * 新建一个页
	 * @return
	 */
	public static Pagination newOnePage(){
		Pagination page = new Pagination();
		page.setCurrentPage(1);
		page.setPageSize(1);
		return page;
	}
}
